#!/bin/bash

# Spring MVC字数统计项目 - 测试脚本

# 设置颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}================================${NC}"
echo -e "${BLUE}  Spring MVC项目测试${NC}"
echo -e "${BLUE}================================${NC}"

# 检查项目结构
echo -e "${YELLOW}1. 检查项目结构...${NC}"

# 检查源代码
if [ -f "src/com/wordcounter/controller/WordCounterController.java" ]; then
    echo -e "${GREEN}✓ 源代码文件存在${NC}"
else
    echo -e "${RED}✗ 源代码文件不存在${NC}"
    exit 1
fi

# 检查编译后的class文件
if [ -f "dist/WEB-INF/classes/com/wordcounter/controller/WordCounterController.class" ]; then
    echo -e "${GREEN}✓ 编译后的class文件存在${NC}"
else
    echo -e "${RED}✗ 编译后的class文件不存在，请先运行 ./build.sh${NC}"
    exit 1
fi

# 检查依赖包
echo -e "${YELLOW}2. 检查依赖包...${NC}"
required_jars=(
    "spring-webmvc-4.1.1.RELEASE.jar"
    "spring-core-4.1.1.RELEASE.jar"
    "spring-context-4.1.1.RELEASE.jar"
    "spring-web-4.1.1.RELEASE.jar"
    "servlet-api-3.1.0.jar"
    "jsp-api-2.3.3.jar"
)

for jar in "${required_jars[@]}"; do
    if [ -f "dist/WEB-INF/lib/$jar" ]; then
        echo -e "${GREEN}✓ $jar${NC}"
    else
        echo -e "${RED}✗ $jar 缺失${NC}"
    fi
done

# 检查配置文件
echo -e "${YELLOW}3. 检查配置文件...${NC}"
config_files=(
    "dist/WEB-INF/web.xml"
    "dist/WEB-INF/spring-mvc-servlet.xml"
    "dist/WEB-INF/views/index.jsp"
    "dist/WEB-INF/views/result.jsp"
    ".tomcat/conf/server.xml"
    ".tomcat/bin/setenv.sh"
)

for config in "${config_files[@]}"; do
    if [ -f "$config" ]; then
        echo -e "${GREEN}✓ $config${NC}"
    else
        echo -e "${RED}✗ $config 缺失${NC}"
    fi
done

# 检查Tomcat
echo -e "${YELLOW}4. 检查Tomcat配置...${NC}"
TOMCAT_HOME="/Users/liuyu/Workbench/Sources/05Deploy/apache-tomcat-9.0.43"
if [ -f "$TOMCAT_HOME/bin/catalina.sh" ]; then
    echo -e "${GREEN}✓ Tomcat可执行文件存在${NC}"
else
    echo -e "${RED}✗ Tomcat可执行文件不存在${NC}"
fi

# 检查端口占用
echo -e "${YELLOW}5. 检查端口占用...${NC}"
if lsof -i :8080 > /dev/null 2>&1; then
    echo -e "${RED}✗ 端口8080已被占用${NC}"
    echo -e "${YELLOW}请先停止占用端口的进程${NC}"
else
    echo -e "${GREEN}✓ 端口8080可用${NC}"
fi

echo ""
echo -e "${GREEN}================================${NC}"
echo -e "${GREEN}  项目测试完成！${NC}"
echo -e "${GREEN}================================${NC}"
echo ""
echo -e "${BLUE}下一步操作：${NC}"
echo -e "${YELLOW}1. 启动应用：./start-tomcat.sh${NC}"
echo -e "${YELLOW}2. 访问地址：http://localhost:8080/wordcounter${NC}"
echo -e "${YELLOW}3. 停止应用：./stop-tomcat.sh${NC}" 